import numpy as np

arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])

print(arr, arr.shape)#shape 范围tupe

arr = np.array([1, 2, 3, 4], ndmin=5)
print(arr, 'shape of array :', arr.shape)

arr = np.array([1, 2, 3, 4, 5])
print(arr, 'shape of array :', arr.shape)

# reshape,变换数组形态，重排的含义，1D->2D
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
print(arr, 'shape of array :', arr.shape)
newarr = arr.reshape(4, 3)
print(newarr, 'shape of array :', newarr.shape)

# reshape:1D to 3D,重点 size相等
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
print(arr, 'shape of array :', arr.shape, 'size : ', arr.size)
newarr = arr.reshape(2, 2,3)
print(newarr, 'shape of array :', newarr.shape, 'size : ', newarr.size)

# 留一个维度不确定，但是不能变量过多，根据size相等的方式做简单算术得到了
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
print(arr, 'shape of array :', arr.shape, 'size : ', arr.size)
newarr = arr.reshape(-1, 2,3) #用-1表示我不想计算了，真的没啥意思
print(newarr, 'shape of array :', newarr.shape, 'size : ', newarr.size)

# Flattening the array，将任意维度的数组reshape到一维
arr = np.array([[1, 2, 3], [4, 5, 6]])
newarr = arr.reshape(-1) #其实跟上面的-1作用一样，我不想计算了
print(newarr, 'shape of array :', newarr.shape, 'size : ', newarr.size)

# reshape is view or copy
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
print(arr, 'shape of array :', arr.shape, 'size : ', arr.size)
newarr = arr.reshape(2, 2,3)
print("arr.reshape(2, 2,3) is copy ?",newarr.base is None) # 语法重点，这里直接用==None会产生混淆。
#print("arr.reshape(2, 2,3) is copy ?",not isinstance(newarr.base, np.ndarray))# 这样也行，就是不能直接跟None 做equal比较